//
//  YYRouterExtension.swift
//  YYProject
//
//  Created by 陆文宇 on 2017/8/18.
//  Copyright © 2017年 陆文宇. All rights reserved.
//

import UIKit

extension YYRoute.Key {
    
    static let login = "/app/login"
    static let message = "/app/message/list"
    static let order = "/app/order/main"
    static let income = "/app/income/list"
    
}

extension YYRouter {
    
    static func initRoutes() -> [YYRoute.Key: YYRoute] {
        return [
            .login: YYRoute(key: "rn", controller: YYController.self, openType: .present, needValidateLogin: false),
            .message: YYRoute(key: "rn", controller: YYController.self, openType: .push, needValidateLogin: true),
            .order: YYRoute(key: "rn", controller: YYController.self, openType: .push, needValidateLogin: true),
            .income: YYRoute(key: "rn", controller: YYController.self, openType: .push, needValidateLogin: true)
        ]
    }
    
    static func getUrlString(routeUrl: String, params: [String: Any]) -> String {
        let routeUrl = routeUrl.hasPrefix("http") ? routeUrl : webviewUrlPrex + routeUrl
        guard !params.isEmpty else {
            return routeUrl
        }
        
        var paramStringArr = [String]()
        for (key, value) in params {
            paramStringArr.append("\(key)=\(value)")
        }
        var paramsString = paramStringArr.joined(separator: "&")
        paramsString = paramsString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
        if let url = URL(string: routeUrl) {
            return url.absoluteString.appending(url.query == nil ? "?\(paramsString)" : "&\(paramsString)")
        }
        return ""
    }
    
    static func validateLogin() -> Bool {
        return true
    }
    
    static func gotoLogin() {
        pushToVC(of: .login, params: [:])
    }
    
}
